package cn.net.cfss.fgbp.face.console.controller;

import cn.net.cfss.fgbp.api.console.dto.SysApplicationDto;
import cn.net.cfss.fgbp.api.console.inte.ISysApplicationService;
import cn.net.cfss.fgbp.api.console.vo.SysApplicationVo;
import cn.net.cfss.fgbp.api.console.vo.SysUserVo;
import cn.net.cfss.fgbp.base.common.GlobalConstants;
import cn.net.cfss.fgbp.base.constant.ErrCodeEnum;
import cn.net.cfss.fgbp.base.controller.BaseController;
import cn.net.cfss.fgbp.face.console.util.ReturnUtil;
import cn.net.cfss.fgbp.face.console.vo.BaseParas;
import cn.net.cfss.fgbp.log.influxdb.ApiDto;
import cn.net.cfss.fgbp.log.statistics.IStatsdLogService;
import cn.net.cfss.fgbp.log.statistics.LogDtoFactory;
import cn.net.cfss.fgbp.log.util.LogStandardUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @author oygq
 * @title: SysApplicationController
 * @description: TODO
 * @date 2019/4/8 11:03
 */
@RestController
@Slf4j
@RequestMapping("console/app")
public class SysApplicationController extends BaseController {
    @Autowired
    ISysApplicationService applicationService;

    @ApiOperation("应用新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String inset(@RequestBody BaseParas paras){
        SysApplicationVo vo = JSONObject.toJavaObject(paras.getPlain(),SysApplicationVo.class);
        if(null == vo){
            return ReturnUtil.returnErr(ErrCodeEnum.ERR_VAII);
        }
        vo.setCreateBy(Long.parseLong(request.getHeader(GlobalConstants.HEADER_USERID)));
        if(!applicationService.uniquenessCheck(vo.getPermission(), vo.getId())){
            return ReturnUtil.returnErr("应用名称或权限标识已存在");
        }
        if(applicationService.insert(vo)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("应用修改")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String update(@RequestBody BaseParas paras){
        SysApplicationVo vo = JSONObject.toJavaObject(paras.getPlain(),SysApplicationVo.class);
        if(null == vo){
            return ReturnUtil.returnErr(ErrCodeEnum.ERR_VAII);
        }
        vo.setUpdateBy(Long.parseLong(request.getHeader(GlobalConstants.HEADER_USERID)));
        if(!applicationService.uniquenessCheck(vo.getPermission(), vo.getAppId())){
            return ReturnUtil.returnErr("应用名称或权限标识已存在");
        }
        if(applicationService.update(vo)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("应用删除")
    @RequestMapping(value = "/delete/{appId}", method = RequestMethod.POST)
    public String update(@PathVariable Long appId){
        if(null == appId){
            return ReturnUtil.returnErr(ErrCodeEnum.ERR_VAII);
        }
        if(applicationService.delete(appId)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("角色批量删除")
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public String deleteBatch(@RequestBody BaseParas paras){
        List<Long> orgIds = (List<Long>) paras.getPlain().get("appIds");
        if(null == orgIds || orgIds.size() == 0){
            return ReturnUtil.returnErr(ErrCodeEnum.ERR_VAII);
        }
        if(applicationService.deleteBatch(orgIds)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("根据id查询应用")
    @RequestMapping(value = "/query/{appId}", method = RequestMethod.POST)
    public String queryById(@PathVariable Long appId){
        if(null == appId){
            return ReturnUtil.returnErr("参数对象为空");
        }
        SysApplicationDto dto = applicationService.queryById(appId);
        return ReturnUtil.returnSucc(dto);
    }

    @ApiOperation("根据条件查询应用")
    @RequestMapping(value = "/query/list", method = RequestMethod.POST)
    public String queryByList(@RequestBody BaseParas paras){
        SysApplicationVo vo = JSONObject.toJavaObject(paras.getPlain().getJSONObject("search"), SysApplicationVo.class);
        if (null == vo){
            return ReturnUtil.returnSucc();
        }
        List<SysApplicationDto> dto = applicationService.queryList(vo);
        return ReturnUtil.returnSucc(dto);
    }

    @ApiOperation("应用的唯一性校验")
    @RequestMapping(value = "/uniquenessCheck", method = RequestMethod.POST)
    public String uniquenessCheck(@RequestBody BaseParas paras) {
        try {
            String permission = paras.getPlain().getString("code");
            Long id = paras.getPlain().getLong("id");
            return ReturnUtil.returnSucc(applicationService.uniquenessCheck(permission,id));
        }catch (Exception e){
            log.error("应用的唯一性校验请求失败！",e.getMessage());
            return ReturnUtil.returnErr("应用的唯一性校验请求失败！");
        }
    }

    /*@ApiOperation("根据角色Id查询权限应用")
    @RequestMapping(value = "/query/{roleId}", method = RequestMethod.POST)
    public String queryByRoleId(HttpServletRequest request,@PathVariable Long roleId){
        List<Long> dto = applicationService.queryByRoleId(roleId);
        return ReturnUtil.returnSucc(dto);
    }*/

}
